package com.opmsys.interceptor;

import com.opmsys.result.VisiteDeny;
import com.opmsys.utils.authEnum.PathEnum;
import com.opmsys.utils.authEnum.StatusEnum;
import com.opmsys.utils.jsonWebToken.JsonWebTokenUtil;
import com.opmsys.utils.redis.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AuthInterceptor implements HandlerInterceptor {
    private static final Logger LOGGER = LoggerFactory.getLogger(LoginInterceptor.class);
    private static final ThreadLocal<Long> START_THREAD_LOCAL = new ThreadLocal<>();

    final private RedisUtil redisUtil;
    final private JsonWebTokenUtil jsonWebTokenUtil;

    public AuthInterceptor(RedisUtil redisUtil, JsonWebTokenUtil jsonWebTokenUtil) {
        this.redisUtil = redisUtil;
        this.jsonWebTokenUtil = jsonWebTokenUtil;
    }

    private boolean match(String key,String uri){
        String regex = ".*"+key+".*";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(uri);
        return matcher.matches();
    }
    private String abstractPathFromURI(String uri) {
        for (PathEnum path : PathEnum.values()) {
            if (match(String.valueOf(PathEnum.ACCIDENT), uri)) {
                return path.toString();
            }
        }
        return "";
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String JWT = request.getHeader("JWT");
        long number = jsonWebTokenUtil.getNumberFromJWT(JWT);
        int status = jsonWebTokenUtil.getStatusFromJWT(JWT);
        String method = request.getMethod();
        String uri = request.getRequestURI();
        String keyPath = abstractPathFromURI(uri);

        //如果用户是管理员，无需鉴权
        if (status == StatusEnum.ADMIN.status){
            return true;
        }

        switch (method){
            case "POST":
                if (keyPath.equals(PathEnum.ACCIDENT.path) && (status==StatusEnum.WORKER.status || status==StatusEnum.FIXER.status)){
                    return true;
                }else if (keyPath.equals(PathEnum.MAINTAIN.path) && status == StatusEnum.FIXER.status){
                    return true;
                }else if(keyPath.equals(PathEnum.REPAIR.path) && status == StatusEnum.FIXER.status){
                    return true;
                } else if(keyPath.equals(PathEnum.TEST.path) && status == StatusEnum.TESTER.status){
                    return true;
                }
            case "GET":
                return true;
            case "PUT":
            case "DEL":
                new VisiteDeny(response,"您不是管理员，无权使用该接口！");
            default:
                new VisiteDeny(response,"不支持您的该类型请求！");
        }

        return false;
    }



    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }
}





